package indi.zhang.securityrbac.framework.interceptor.impl;

import com.alibaba.fastjson2.JSON;
import indi.zhang.securityrbac.core.domain.AjaxResult;
import indi.zhang.securityrbac.utils.DefaultValueUtils;
import indi.zhang.securityrbac.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@Component
@RestControllerAdvice("indi.zhang.**")
public class LocalAuthResponseInterceptor implements ResponseBodyAdvice<Object> {

    private static final Logger log = LoggerFactory.getLogger(LocalAuthResponseInterceptor.class);

    /**
     * Whether this component supports the given controller method return type
     * and the selected {@code HttpMessageConverter} type.
     *
     * @param returnType    the return type
     * @param converterType the selected converter type
     * @return {@code true} if {@link #beforeBodyWrite} should be invoked;
     * {@code false} otherwise
     */
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return false;
    }

    /**
     * Invoked after an {@code HttpMessageConverter} is selected and just before
     * its write method is invoked.
     *
     * @param body                  the body to be written
     * @param returnType            the return type of the controller method
     * @param selectedContentType   the content type selected through content negotiation
     * @param selectedConverterType the converter type selected to write to the response
     * @param request               the current request
     * @param response              the current response
     * @return the body that was passed in or a modified (possibly new) instance
     */
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body!=null) {
            log.info(body.toString());
        }

        // String类型不能直接包装，需要特别处理, 请思考为什么
        if (returnType.getParameterType().equals(String.class)) {
            DefaultValueUtils.setNullDefault(body);
            return body;
            /*ObjectMapper objectMapper = new ObjectMapper();
            try {
                // 将数据包装在JsonResponse里返回
                return objectMapper.writeValueAsString(AjaxResult.success(body));
            } catch (JsonProcessingException e) {
                throw new RuntimeException("返回String类型错误");
            }*/
        }

        if (StringUtils.equals(returnType.getParameterType().getName(), "void") && selectedContentType.equals(MediaType.APPLICATION_JSON)) {
            return AjaxResult.success("操作成功");
        }

        if (returnType.getParameterType().equals(AjaxResult.class)) {
            String bodyStr = JSON.toJSONString(body);
            AjaxResult ajaxResult = JSON.parseObject(bodyStr,AjaxResult.class);
            Object data = ajaxResult.get("data");
            DefaultValueUtils.setNullDefault(data);
            return ajaxResult;
        }

        DefaultValueUtils.setNullDefault(body);
        return body;

    }
}
